package com.education.teach.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.di.component.DaggerHomeComponent;
import com.education.teach.di.module.HomeModule;
import com.education.teach.mvp.contract.HomeContract;
import com.education.teach.mvp.model.bean.AddCounselBean;
import com.education.teach.mvp.model.bean.LoginBean;
import com.education.teach.mvp.model.bean.ReportBean;
import com.education.teach.mvp.model.service.ApiService;
import com.education.teach.mvp.presenter.HomePresenter;
import com.education.teach.mvp.ui.activity.AddCounselActivity;
import com.education.teach.mvp.ui.adapter.AddCounselAdapter;
import com.education.teach.utils.DnCommonUtils;
import com.education.teach.utils.FileUtils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dn.easy.common.constant.Contans;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class HomeFragment extends BaseFragment<HomePresenter> implements HomeContract.View {

    @BindView(R.id.sp_teacher)
    Spinner sp_teacher;
    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.tv_report)
    TextView tv_report;
    @BindView(R.id.tv_yijian)
    TextView tv_yijian;
    @BindView(R.id.tv_fenxi)
    TextView tv_fenxi;
    private List<AddCounselBean.ResultBean> mList = new ArrayList<>();
//    private AddCounselAdapter mAdapter;
    private ArrayList<String> teacherListId = new ArrayList<String>();
    private String selected;
    @Inject
    IRepositoryManager mIRepositoryManager;
    @Inject
    RxErrorHandler mRxErrorHandler;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("学业报告");
        // 读取教师列表
        getData();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    private void getData() {

        Map<String, String> map = new HashMap<>();
        LoginBean bean = DnCommonUtils.getUserModel(this.getActivity());
        map.put("banjiId", bean.getResult().getClazzIdList().get(0));// banjiId是登录里的clazzid
        mIRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .getTeacher(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<AddCounselBean>(mRxErrorHandler) {
                    @Override
                    public void onNext(AddCounselBean addCounselBean) {
//                        mAdapter.setNewData(addCounselBean.getResult());
                        mList = addCounselBean.getResult();
                        ArrayList<String> teacherList = new ArrayList<String>();
//                        teacherList.add("请选择您要提问的老师");
//                        teacherListId.add("");
                        for (int i = 0; i < mList.size(); i++) {
                            AddCounselBean.ResultBean bean = mList.get(i);
                            teacherList.add(bean.getJiaoshiName()+"的意见与分析");
                            teacherListId.add(bean.getJiaoshiSign());
                        }
                        ArrayAdapter adapter2 = new ArrayAdapter(HomeFragment.this.getActivity(), R.layout.item_teacher, R.id.tv_teacher, teacherList);
                        sp_teacher.setAdapter(adapter2);
                        sp_teacher.setOnItemSelectedListener(new spinner2Listener());
                    }
                });
    }

    class spinner2Listener implements android.widget.AdapterView.OnItemSelectedListener {


        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            LoginBean loginBean = DnCommonUtils.getUserModel(HomeFragment.this.getActivity());
            selected = teacherListId.get(position).toString();
            Map<String, String> map = new HashMap<>();
            map.put("jsId", selected);
            map.put("studentid", loginBean.getResult().getXsXlh());
            mIRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .searchReport(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> showLoading())
                .doFinally(() -> hideLoading())
                .subscribe(new ErrorHandleSubscriber<ReportBean>(mRxErrorHandler) {
                    @Override
                    public void onNext(ReportBean reportBean) {
                        if (reportBean.getResult() != null){
                            tv_report.setText(reportBean.getResult().getPjneirong());
                            tv_yijian.setText(reportBean.getResult().getPjyj());
                            tv_fenxi.setText(reportBean.getResult().getPjfx());
                        }else{
                            tv_report.setText("老师还没有对您发布学习报告，请耐心等待。");
                            tv_yijian.setText("老师还没有对您发布意见，请耐心等待。");
                            tv_fenxi.setText("老师还没有对您发布成绩分析，请耐心等待。");
                        }
                    }
                });
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            System.out.println("nothingSelect");
        }
    }
}
